package leetcode.D1000.T974;

/**
 * @Author: 18362
 * @Create: 2022-09-21 15:14:29 星期三
 */
class Solution {
    public int subarraysDivByK(int[] nums, int k) {
        int[] cnt = new int[k];
        cnt[0] = 1;
        int sum = 0, res = 0;
        for (int i=0; i<nums.length; ++i) {
            sum += nums[i];
            int key = (sum % k + k) % k;
            res += cnt[key];
            cnt[key]++;
        }
        return res;
    }
}
